<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!--{row}解构obj, obj表示当前state的数据 -->
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!-- 分页组件里面决定页数,每页条数,数据总数,切换分页事件 -->
              <el-pagination layout="prev,pager,next" :current-page="page.page" :page-size="page.pagesize" :total="page.total" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog :title="roleForm.id?'编辑':'新增角色'" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" type="textarea" :rows="3" />
          </el-form-item>
        </el-form>
        <!-- 弹窗底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确认</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 放置分配权限弹窗 -->
      <el-dialog title="分配权限" :visible="showPerm" @close="btnPermCancel">
        <!-- 树形组件属性:
          show-checkbox显示复选框
          check-strictly 表示子项不选中,不会影响父项
          node-key 告诉树形组件传递的数据是id
         -->
        <el-tree
          ref="permTree"
          :data="permList"
          :props="{label:'name'}"
          default-expand-all
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
        />
        <!-- footer -->
        <el-row slot="footer" type="flex" justify="end">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnSavePerm">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, addRole, getRoleDetail, editRole, assignRolePerms } from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      showPerm: false, // 显示隐藏弹窗
      permList: [],
      companyInfo: [],
      roleList: [], // 角色列表
      // 放置页面数据
      page: {
        page: 1, // 角色列表页数
        pagesize: 2, // 每页数据条数
        total: 0 // 记录总数
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }
    }
  },

  created() {
    this.getCompanyInfo() // 在创建周期获取公司信息
    this.getRoleList() // 在创建周期获取角色列表数据
    this.getPermList() // 获取权限数据
  },

  methods: {
    // 保存权限修改
    async btnSavePerm() {
      // 上传选中权限数据
      await assignRolePerms({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('保存成功')
      this.showPerm = false
    },

    btnPermCancel() {
      this.showPerm = false
      // 清空选中
      this.$refs.permTree.setCheckedKeys([])
    },

    // 获取权限
    async getPermList() {
      // 传入字符串'0'作为标记,pid为0表示顶级权限
      this.permList = listToTreeData(await getPermList(), '0')
    },

    // 分配权限
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.showPerm = true
      // 等待页面渲染树形数据完毕,才渲染选中的权限
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.getters.companyId) // 传入getters映射的companyId
      // console.log(this.companyInfo)
    },
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.roleList = rows
      // console.log(this.roleList)
      this.page.total = total
    },
    // 切换分页
    changePage(index) {
      this.page.page = index
      this.getRoleList() // 每切换一次获取一次角色数据
    },

    // 删除角色
    async deleteRole(id) {
      // console.log(id)
      try {
        // 确认是否删除
        await this.$confirm('是否删除角色?')
        await deleteRole(id)
        await this.getRoleList() // 重新获取列表
        console.log(this.page.page)
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 点击新增按钮
    addRole() {
      this.showDialog = true
    },
    // 点击编辑按钮
    async  editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },

    // 弹窗确认
    async btnOK() {
      try {
        // 校验用户输入数据
        await this.$refs.roleForm.validate()
        // 发请求
        if (this.roleForm.id) {
          await editRole(this.roleForm)
        } else {
        //  新增角色
          await addRole(this.roleForm)
        }
        // 提示用户成功
        this.$message.success('更新成功')
        // 更新页面
        this.getRoleList()
        // 关闭弹窗
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },

    // 弹窗取消按钮
    btnCancel() {
      // 清除数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清除错误
      this.$refs.roleForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
